package com.chb.mains;

import java.util.Stack;

/**
 * @description:
 * @author: CHB
 * @create: 2022-04-09 14:22
 **/
public class SolutionIsPalindrome {
    // 给你一个单链表的头节点 head ，请你判断该链表是否为回文链表。如果是，返回 true ；否则，返回 false 。
    public static void main(String[] args) {
        ListNode listNode = new ListNode(1,new ListNode(2,new ListNode(2,new ListNode(1))));
        System.out.println(isPalindrome(listNode));
    }
    public static boolean isPalindrome(ListNode head) {
        ListNode listNode = head;
        Stack<ListNode> stack = new Stack();
        // 进栈
        while (listNode != null){
            stack.push(listNode);
            listNode = listNode.next;
        }
        while(!stack.isEmpty()) {
            if(stack.pop().val != head.val){
                return false;
            }
            head = head.next;
        }
        return true;
    }
}
 class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     @Override
     public String toString(){
        StringBuilder s = new StringBuilder();
        ListNode node = this;
        while (true){
            if(node == null){
                break;
            }
            s.append(node.val + ",");
            node = node.next;
        }
        return s.toString();
     }
}